library(readxl)
library(dplyr)
library(readstata13)

main_final=readstata13::read.dta13("main_final.dta")
contract_17 = main_final %>% filter(year == 2017)
contract_18 = main_final %>% filter(year == 2018)

#####################################
# Figure C1 
#####################################

# (a) 
price18 = readxl::read_xlsx("price_18_house.xlsx")
price18$Contract = stringr::str_trim(price18$Contract)  
price18$inc_win = ifelse(price18$Contract %in% c("RH.gain18","RH.hold18"),1,0)
price18$Date =format(as.Date(price18$Date,format="%Y-%m-%d"), "%y-%m-%d")
data18 =as.data.frame(tapply(price18$LastPrice, list(price18$Date, price18$inc_win), sum, na.rm=T))
colnames(data18) = c("inc_lose","inc_win")

data18$date = row.names(data18)
colnames(data18)[3]="action_date"

predict = readxl::read_excel("predictit.xlsx")
predict_house =subset(predict, `Market ID`==2704&`Contract Name`=="Democratic")#
predict_house$`Date (ET)` =as.Date(predict_house$`Date (ET)`, origin = "1899-12-30")
predict_house$`Date (ET)` =format(as.Date(predict_house$`Date (ET)`,format="%Y-%m-%d"), "%y-%m-%d")
predict_house= predict_house[,c("Date (ET)","Close Share Price")]
colnames(predict_house)=c("action_date","inc_lose2")
plot(predict_house$inc_lose2, type="l",ylim=c(0,1))
abline(h=0.5, col="grey20",lty=3)

data= merge(data18,predict_house, by="action_date",all=T)
data= data[as.Date(data$action_date, format="%y-%m-%d")>=as.Date("2017-01-01"),]

dev.off()
par(mar=c(3,5.5,1,2))
plot(data$inc_lose, type="l",ylim=c(0,1),axes=F,xlab="",ylab="");box()
lines(data$inc_lose2, type="l",lty=2,col="red")
#lines(inc_lose, type="l",lty=2, ylim=c(0,1.05),col="red")
axis(side=1,at=1:nrow(data), labels=data$action_date,las=1) #ADD X-AXIS LABELS WITH "YEAR-MONTH" FORMAT
axis(side=2,at=seq(0,1,by=0.1), labels=seq(0,1,by=0.1),las=2) #ADD Y-AXIS LABELS
mtext('Date',side=1,line=5) #X-AXIS LABEL
mtext('Pr(Incumbent Losing House)',side=2,line=4, cex=1.5) #Y-AXIS LABEL
abline(h=0.5, col="grey20",lty=3)
legend("topleft",col=c("black","red"),lty=c(1,2),legend=c("IEM","PredictIt"),cex=1.1)


#(b)
data=unique(contract_17[,c("action_date","inc_lose")])
data2=unique(contract_18[,c("action_date","inc_lose")])
data=rbind(data,data2)
data$action_date=format(as.Date(data$action_date, "%Y-%m-%d"),"%y-%m-%d")

predict = readxl::read_excel("predictit.xlsx")
predict_senate =subset(predict, `Market ID`==2703&`Contract Name`=="Democratic")#
predict_senate$`Date (ET)` =as.Date(predict_senate$`Date (ET)`, origin = "1899-12-30")
predict_senate$`Date (ET)` =format(as.Date(predict_senate$`Date (ET)`,format="%Y-%m-%d"), "%y-%m-%d")
predict_senate= predict_senate[,c("Date (ET)","Close Share Price")]
colnames(predict_senate)=c("action_date","inc_lose2")

data= merge(data,predict_senate, by="action_date",all=T)
data= data[as.Date(data$action_date, format="%y-%m-%d")>=as.Date("2017-01-01"),]

dev.off()
par(mar=c(3,5.5,1,2))
plot(data$inc_lose, type="l",ylim=c(0,1),axes=F,xlab="",ylab="");box()
lines(data$inc_lose2, type="l",lty=2,col="red")
#lines(inc_lose, type="l",lty=2, ylim=c(0,1.05),col="red")
axis(side=1,at=1:nrow(data), labels=data$action_date,las=1) #ADD X-AXIS LABELS WITH "YEAR-MONTH" FORMAT
axis(side=2,at=seq(0,1,by=0.1), labels=seq(0,1,by=0.1),las=2) #ADD Y-AXIS LABELS
mtext('Date',side=1,line=5) #X-AXIS LABEL
mtext('Pr(Incumbent Losing Senate)',side=2,line=4, cex=1.5) #Y-AXIS LABEL
abline(h=0.5, col="grey20",lty=3)
legend("topright",col=c("black","red"),lty=c(1,2),legend=c("IEM","PredictIt"),cex=1.1)
